package com.kbs.im.server.handler;

import com.kbs.im.common.protocol.packet.request.DeviceRegisterRequestPacket;
import com.kbs.im.common.protocol.packet.response.DeviceRegisterResponsePacket;
import com.kbs.im.server.utils.SessionManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 设备注册请求处理
 *
 * @author zhangzhang
 */
@ChannelHandler.Sharable
public class DeviceRegisterRequestHandler extends SimpleChannelInboundHandler<DeviceRegisterRequestPacket> {

    public static final DeviceRegisterRequestHandler INSTANCE = new DeviceRegisterRequestHandler();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DeviceRegisterRequestPacket msg) throws Exception {
        System.out.println(msg.getDeviceCPU());
        SessionManager.DEVICE_CHANNEL_MAP.put(msg.getDeviceCPU(), ctx.channel());
        DeviceRegisterResponsePacket responsePacket = new DeviceRegisterResponsePacket();
        responsePacket.setCode("200");
        responsePacket.setMsg("设备注册成功！");
        ctx.channel().writeAndFlush(responsePacket);
    }
}
